<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Логин</title>
</head>
<body>

<form id="loginForm">
    <h1 id="succes"></h1>
    <input type="email" id="email" placeholder="Email" required>
    <input type="password" id="password" placeholder="Password" required>
    <button type="submit">Отправить</button>
</form>

<script>
const form = document.getElementById('loginForm');

form.addEventListener('submit', function(event) {
    event.preventDefault();

    const email = document.getElementById('email').value;
    const password = document.getElementById('password').value;

    const data = JSON.stringify({
        email: email,
        password: password
    });

    const params = {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: data
    };

    fetch('/login', params)
        .then(response => response.json())
        .then(data => {
            if (data.id) {
                setCookie('id', data.id, 1); // Устанавливаем cookie с идентификатором пользователя
                document.getElementById("succes").innerText = `Успешная авторизация для пользователя с id ${data.id}!`;
                console.log('Cookie установлен:', data.id);
            } else {
                document.getElementById("succes").innerText = `Ошибка авторизации!`;
                console.log('Ошибка авторизации:', data);
            }
        })
        .catch((error) => console.error('Ошибка:', error));
});

function setCookie(name, value, days) {
    let expires = "";
    if (days) {
        let date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        expires = "; expires=" + date.toUTCString();
    }
    document.cookie = name + "=" + (value || "") + expires + "; path=/";
}
</script>

</body>
</html>